package com.zhj.rabbitmq.demo4;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @program: rabbimmq-demo
 * @description: topic类型交换机 消息1
 * @author: zhengjibin
 * @create: 2020-11-17 11:54
 * @Version 1.0
 **/
public class ExchangeTopicReceiveMessage04 {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        //配置rabbitmq的链接信息
        factory.setHost("122.112.198.94");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = null;// 定义rabbitmq的网络连接 ，一个连接
        Channel channel = null;//定义一个链接中的通道
        connection = factory.newConnection();//通过工厂获得连接
        channel = connection.createChannel();//通过连接获得通道
        String queueName = "topic4_queue";
        channel.queueDeclare(queueName,true,false,false,null);
        String exchangeName = "topic_exchange";
        channel.exchangeDeclare(exchangeName,"topic",true);
        String routingkey = "aa.#";
        channel.queueBind(queueName,exchangeName,routingkey);
        channel.basicConsume(queueName,true,"",new DefaultConsumer(channel){
            //重写 DefaultConsumer 中的方法
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //super.handleDelivery(consumerTag, envelope, properties, body);
                String message = new String(body,"UTF-8");
                System.out.println("topic消费者04 aa.#："+message);
            }
        });
    }
}
